package us.wili.dev.common.model.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * Created by JasonY on 17/2/19.
 */
public class UnificationNoValidator implements ConstraintValidator<UnificationNo, Object> {

    private final static String UNIFICATION_NO_REG = "^[^_IOZSVa-z\\W]{2}\\d{6}[^_IOZSVa-z\\W]{10}$";
    private Pattern unificationPattern = Pattern.compile(UNIFICATION_NO_REG);

    @Override
    public void initialize(UnificationNo unificationNo) {

    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }

        if (!(value instanceof String)) {
            return false;
        }

        return unificationPattern.matcher(value.toString()).matches();
    }
}
